﻿using System.Collections.Generic;

namespace NHibData.Repository
{
    /// <summary>
    /// Factory for creating repositories of different types
    /// </summary>
    public static class RepositoryFactory
    {
        /// <summary>
        /// Dictionary of c
        /// </summary>
        private static readonly IDictionary<string, object> RepositoryDictionary = new Dictionary<string, object>();


        /// <summary>
        /// Creates repository for given DomainType
        /// </summary>
        /// <typeparam name="T">Domain type</typeparam>
        /// <returns></returns>
        public static IRepository<T> Create<T>() where T: BaseEntity
        {
            var typeName = typeof (T).ToString();
            if (RepositoryDictionary.ContainsKey(typeName) == false)
            {
                RepositoryDictionary[typeName] = new NHibernateRepository<T>();
            }
            return RepositoryDictionary[typeName] as IRepository<T>;
        }

        public static IRepository<T> Create<T>(string config) where T : BaseEntity
        {
            var typeName = typeof(T).ToString();
            if (RepositoryDictionary.ContainsKey(typeName) == false)
            {
                RepositoryDictionary[typeName] = new NHibernateRepository<T>(config);
            }
            return RepositoryDictionary[typeName] as IRepository<T>;
        }
    }
}